package com.anlogic.sdk.efuse.model;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;

import com.anlogic.sdk.utils.ECCUtils;

public class ProgramEfuseModel {
	private boolean enableSecondGroup;
	private boolean verify;
	private boolean revokeFirstEncKey;
	private boolean revokeFirstPpk;
	private boolean trngWrite;
	
	private String firstEncryptyKey;
	private String firstPpk;
	private String firstEncType;
	private String firstAuthType;
	private boolean firstEncKeyWriteLock;
	private boolean firstPpkWriteLock;
	
	private String secondEncryptyKey;
	private String secondPpk;
	private String secondEncType;
	private String secondAuthType;
	private boolean secondEncKeyWriteLock;
	private boolean secondPpkWriteLock;

	public boolean getEnableSecondGroup() {
		return enableSecondGroup;
	}
	
	public void setEnableSecondGroup(boolean enableSecondGroup) {
		this.enableSecondGroup = enableSecondGroup;
	}
	
	public boolean getVerify() {
		return verify;
	}
	
	public void setVerify(boolean verify) {
		this.verify = verify;
	}
	
	public boolean getRevokeFirstEncKey() {
		return this.revokeFirstEncKey;
	}
	
	public void setRevokeFirstEncKey(boolean revoke) {
		this.revokeFirstEncKey = revoke;
	}
	
	public boolean getRevokeFirstPpk() {
		return this.revokeFirstPpk;
	}
	
	public void setRevokeFirstPpk(boolean revoke) {
		this.revokeFirstPpk = revoke;
	}
	
	public String getEncryptyKey(GroupId groupId) {
		String ret = firstEncryptyKey;
		if (groupId == GroupId.FIRST) {
			ret = firstEncryptyKey;
		} else if (groupId == GroupId.SECOND) {
			ret = secondEncryptyKey;
		}
		return ret;
	}

	public void setEncryptyKey(GroupId groupId, String EncryptyKey) {
		if (groupId == GroupId.FIRST) {
			firstEncryptyKey = EncryptyKey;
		} else if (groupId == GroupId.SECOND) {
			secondEncryptyKey = EncryptyKey;
		}
	}
	
	public String getPpk(GroupId groupId) {
		String ret = firstPpk;
		if (groupId == GroupId.FIRST) {
			ret = firstPpk;
		} else if (groupId == GroupId.SECOND) {
			ret = secondPpk;
		}
		return ret;
	}

	public void setPpk(GroupId groupId, String ppk) {
		if (groupId == GroupId.FIRST) {
			firstPpk = ppk;
		} else if (groupId == GroupId.SECOND) {
			secondPpk = ppk;
		}
	}
	
	public String getEncType(GroupId groupId) {
		String ret = firstEncType;
		if (groupId == GroupId.FIRST) {
			ret = firstEncType;
		} else if (groupId == GroupId.SECOND) {
			ret = secondEncType;
		}
		return ret;
	}

	public void setEncType(GroupId groupId, String encType) {
		if (groupId == GroupId.FIRST) {
			firstEncType = encType;
		} else if (groupId == GroupId.SECOND) {
			secondEncType = encType;
		}
	}
	
	public String getAuthType(GroupId groupId) {
		String ret = firstAuthType;
		if (groupId == GroupId.FIRST) {
			ret = firstAuthType;
		} else if (groupId == GroupId.SECOND) {
			ret = secondAuthType;
		}
		return ret;
	}

	public void setAuthType(GroupId groupId, String authType) {
		if (groupId == GroupId.FIRST) {
			firstAuthType = authType;
		} else if (groupId == GroupId.SECOND) {
			secondAuthType = authType;
		}
	}
	
	public boolean getEncKeyWriteLock(GroupId groupId) {
		boolean ret = firstEncKeyWriteLock;
		if (groupId == GroupId.FIRST) {
			ret = firstEncKeyWriteLock;
		} else if (groupId == GroupId.SECOND) {
			ret = secondEncKeyWriteLock;
		}
		return ret;
	}

	public void setEncKeyWriteLock(GroupId groupId, boolean lock) {
		if (groupId == GroupId.FIRST) {
			firstEncKeyWriteLock = lock;
		} else if (groupId == GroupId.SECOND) {
			secondEncKeyWriteLock = lock;
		}
	}
	
	public boolean getPpkWriteLock(GroupId groupId) {
		boolean ret = firstPpkWriteLock;
		if (groupId == GroupId.FIRST) {
			ret = firstPpkWriteLock;
		} else if (groupId == GroupId.SECOND) {
			ret = secondPpkWriteLock;
		}
		return ret;
	}

	public void setPpkWriteLock(GroupId groupId, boolean lock) {
		if (groupId == GroupId.FIRST) {
			firstPpkWriteLock = lock;
		} else if (groupId == GroupId.SECOND) {
			secondPpkWriteLock = lock;
		}
	}
	
	public String getEncKeyHashValue(GroupId groupId) {
		String hashValue = "";
		
		String encKeyFile = getEncryptyKey(groupId);
		
		if (encKeyFile == null || encKeyFile.isEmpty()) {
			return hashValue;
		}
		
		hashValue = ECCUtils.getEnctriptKeyHashValueAlign64(encKeyFile);
		
		hashValue = ECCUtils.reverseEndOrder(hashValue);
		
		return hashValue;
	}
	
	public String getPpkHashValue(GroupId groupId) {
		String hashValue = "";
		
		String ppkFile = getPpk(groupId);
		
		if (ppkFile == null || ppkFile.isEmpty()) {
			return hashValue;
		}
		
		try {
			hashValue = ECCUtils.getPublicKeyHashValue(ppkFile);
		} catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchProviderException e) {
			return hashValue;
		}
		
		hashValue = ECCUtils.reverseEndOrder(hashValue);
		
		return hashValue;
	}
	
	public boolean getTRNGWrite() {
		return trngWrite;
	}

	public void setTRNGWrite(boolean value) {
		trngWrite = value;
	}
}
